package cst.a931xmc.yuejian20.Fragment.Me;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import cst.a931xmc.yuejian20.Activity.LoginActivity;
import cst.a931xmc.yuejian20.Activity.MyFollowActivity;
import cst.a931xmc.yuejian20.Activity.MyOrderActivity;
import cst.a931xmc.yuejian20.Activity.MyShowActivity;
import cst.a931xmc.yuejian20.Activity.UpdateInfoActivity;
import cst.a931xmc.yuejian20.Bean.UserBean;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.Model.UserModel;
import cst.a931xmc.yuejian20.R;
import de.hdodenhof.circleimageview.CircleImageView;

public class MeFragment extends Fragment implements View.OnClickListener{
    private Context context;
    private CircleImageView user_pic;
    private TextView user_name;
    private TextView user_province;
    private TextView user_city;
    private TextView user_intro;
    private LinearLayout btn_myfollow;
    private Button btn_update;
    private Button btn_myorder;
    private Button btn_myshow;
    private Button btn_logout;
    private Listener<UserBean> listener;
    private UserModel model;
    int user_id;
    SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_me,container,false);
        initViews(view);
        initData();
        model = new UserModel();
        model.getUserDetail(user_id,listener);
        return view;
    }

    public void initViews(View view){
        user_pic = view.findViewById(R.id.user_pic);
        user_name = view.findViewById(R.id.user_name);
        user_province = view.findViewById(R.id.user_province);
        user_city = view.findViewById(R.id.user_city);
        user_intro = view.findViewById(R.id.user_intro);
        btn_myfollow = view.findViewById(R.id.btn_myfollow);
        btn_update = view.findViewById(R.id.btn_update);
        btn_myorder = view.findViewById(R.id.btn_myorder);
        btn_myshow = view.findViewById(R.id.btn_myshow);
        btn_logout = view.findViewById(R.id.btn_logout);
        btn_myfollow.setOnClickListener(this);
        btn_update.setOnClickListener(this);
        btn_myorder.setOnClickListener(this);
        btn_myshow.setOnClickListener(this);
        btn_logout.setOnClickListener(this);
        sp = this.getActivity().getSharedPreferences("user_id", Context.MODE_PRIVATE);
        user_id = sp.getInt("user_id",0);
    }

    public void initData(){
        listener = new Listener<UserBean>() {
            @Override
            public void onResponse(UserBean list) {
                if (list.getUser_id()!=0){
                    user_name.setText(list.getUser_name());
                    user_province.setText(list.getUser_province());
                    user_city.setText(list.getUser_city());
                    user_intro.setText(list.getUser_intro());
                    ApiModel apiModel = new ApiModel();
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/posters/" + list.getUser_pic())
                            .placeholder(R.drawable.nopic)
                            .into(user_pic);
                }
            }

            @Override
            public void onFail(String message) {

            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_myfollow:
                Intent intent = new Intent(this.getActivity(),MyFollowActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_update:
                intent = new Intent(this.getActivity(),UpdateInfoActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_myorder:
                intent = new Intent(this.getActivity(),MyOrderActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_myshow:
                intent = new Intent(this.getActivity(),MyShowActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_logout:
                editor = sp.edit();
                editor.clear();
                intent = new Intent(this.getActivity(),LoginActivity.class);
                startActivity(intent);
                break;
        }
    }
}
